package com.sk.waste.app.wxapi

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Window
import com.lc.smc.lib.utils.LCBaseUtil.showToast
import com.sk.pay.lib.PayConfig
import com.sk.waste.app.application.MyApplication
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory

/**
 *  @time 2025/5/14  15:10
 *  @author Im_jingwei
 *  @desc 微信支付回调
 */
class WXPayEntryActivity : Activity(), IWXAPIEventHandler {

    private var api: IWXAPI? = null

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        api = WXAPIFactory.createWXAPI(this, PayConfig.APPID_WECHAT)
        api!!.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        api!!.handleIntent(intent, this)
    }

    override fun onReq(req: BaseReq) {}
    override fun onResp(resp: BaseResp) {
        val code = resp.errCode
        if (code == 0) {
            MyApplication.instance.getUserInfo()!!.vip = "VIP"
            showToast(this, "支付成功，您的权益已开通可正常使用！")
        }
        if (code == -1) {
            showToast(this, "支付异常,为保证您的资金安全请勿重复操作，请联系平台客服核实您的支付情况。")
            finish()
        }
        if (code == -2) {
            showToast(this, "支付已取消!")
            finish()
        }
    }
}